<?php
/*
	插件概览
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'get';

if($_RQ['op']=='get'){
	$return['item']=array();
	$htmls=array(
			'type'=>'edit',
			'post'=>'post',
			'desc'=>'下载对账单（已支付）',
			'edit'=>array(
					array('type'=>'radio','title'=>'账单类型','name'=>'type','options'=>'types' ),
					array('type'=>'radio','title'=>'数据类型','name'=>'datatype','options'=>'datatypes' ),
					array('type'=>'time_range','title'=>'支付时间','name'=>'createtime' ),
				)
	);
	$return['types']=array('1'=>'所有账单','2'=>'支付账单','3'=>'退款帐单','4'=>'撤销账单');
	$return['datatypes']=array('1'=>'所有账单','2'=>'仅商城账单');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$post=get_postdata();
	$_SESSION['e2cpost']=$post;
	$url=download_url('jy_weishop/finance.downloadbill/export/');
	exi('','',$url);
}
if($_RQ['op']=='export'){	
	$types=array('1'=>'所有账单','2'=>'支付账单','3'=>'退款帐单','4'=>'撤销账单');
	$datatypes=array('1'=>'所有账单','2'=>'仅商城账单');
	$typestitle = $types[$_SESSION['e2cpost']['type']];
	$datatypestitle = $datatypes[$_SESSION['e2cpost']['datatype']];
	$params=array('uniacid'=>$_SESSION['uniacid'],'paytime >'=>1);
	if($_SESSION['e2cpost']['type'] ==2){
		$params['ispay']=1;
	}
	if($_SESSION['e2cpost']['type'] ==3){
		$params['status']=6;
	}
	if($_SESSION['e2cpost']['type'] ==4){
		$params['status']=5;
	}
	//创建时间
	if($_SESSION['e2cpost']['createtime']){
		//$time_range=cfc('tools')->base64_decode($_SESSION['e2cpost']['createtime'],$_SESSION['base_key_str']);
		//$time_range=json_decode($time_range,true);
		$time_range=$_SESSION['e2cpost']['createtime'];
		$params['paytime between']=array($time_range['tstart'],$time_range['tend']);
		$return['params']['paytime']=$time_range;
	}
	$status=array('1'=>'待发货','2'=>'待收货','3'=>'已完成','4'=>'已关闭','5'=>'申请维权','6'=>'已维权');
	$buytype=array('0'=>'普通','1'=>'多商户','2'=>'货到付款','3'=>'自提','4'=>'加星','5'=>'砍价','6'=>'拼团','7'=>'秒杀','8'=>'抽奖','9'=>'夺宝');
	$list=pdo_getall('jy_weishop_order',$params,array(),'','createtime asc');
	if($list){
		$goodslist=array();
		foreach($list as &$v){
			$v['nickname']=pdo_getcolumn('core_members',array('id'=>$v['mid'],'uniacid'=>$_SESSION['uniacid']),'nickname');
			if(!$v['nickname']){
				$v['nickname']='匿名用户';
			}
			$v['paytime']=date('Y-m-d H:i:s',$v['paytime']);
			$v['status'] = $status[$v['status']];
			$v['buytype'] = $buytype[$v['buytype']];
			$v['ispay'] = $v['ispay'] ==1 ? '已支付' : '未支付';
			$v['paytype'] = get_pay_types()[$v['paytype']];
			$ordergoods=pdo_getall('jy_weishop_order_goods',array('orderid'=>$v['id'],'uniacid'=>$_SESSION['uniacid']),array('id','status','istaocan','goodsid','optionid','price','total','goodssn','productsn'));
			$goodsinfo='';
			foreach($ordergoods as $k=>$gv){
				$num=$k+1;
				$goods = pdo_get('jy_weishop_goods',array('id'=>$gv['goodsid'],'uniacid'=>$_SESSION['uniacid']),array('id','unit','title'));
				$goods['unit']=$goods['unit']?:'件';
				if($k){
					$v=array();
				}
				$v['goodsinfo']='【商品'.$num.'】'.$goods['title'];
				$goodsinfo .=$v['goodsinfo'];
				$v['total']=$gv['total'];
				$goodslist[]=$v;
			}
			$v['goodsinfo']=$goodsinfo;			
		}
	}
	$header=array(
		'id'=>'订单ID',
		'ordersn'=>'订单号',
		'mid'=>'买家ID',
		'nickname'=>'买家',
		'price'=>'金额',
		'goodsinfo'=>'商品',
		'total'=>'数量',
		'status'=>'订单状态',
		'buytype'=>'订单类型',
		'paytime'=>'支付时间',
		'ispay'=>'是否支付',
		'paytype'=>'支付方式',
	);
	cfc('tools')->export2excel($header,$goodslist,$typestitle.$datatypestitle.'-对账单'.date('Y/m/d',time()));
	
}